home *** CD-ROM | disk | FTP | other *** search
Text File | 1989-08-22 | 3.0 KB | 80 lines | [TEXT/GEOL] |
- Item 9114656 20-July-89 16:36
-
- From: ALGER Alger, Jeff
-
- To: ROSENSTEIN1 Rosenstein, Larry
- ARBOGAST Peat Marwick, Chris Arbogast
-
- cc: MACAPP.TECH$ MACAPP Tech
-
- Sub: Re Typecasting
-
- Larry and Chris,
-
- I think you are both right to an extent regarding type casts in Object Pascal.
- The casting code generated by the compiler is consistent with Pascal's roots as
- a (relatively) strongly typed language. Larry, your point is well made that
- this strong typing is one of the central reasons many people prefer Pascal over
- a weakly typed language like C. Chris, you are correct that not all
- programmers agree that strong typing yields a positive cost-benefit. On
- balance, since the domain of discourse here is Pascal I have to agree with
- Larry: if you dislike strong typing, use a less strongly typed language than
- Pascal.
-
- Second observation: a cast of NIL should be legal in all cases. The usual
- treatment of NIL from a formal semantics point of view is that NIL means
- "unknown", not "nothing." This is the reason that NIL is a valid value for
- every object class variable or expression. By consequence, NIL should typecast
- to every class.
-
- Much of the discussion has revolved around examples where the value of a
- completely unitialized variable is assigned to some other variable:
-
- PROCEDURE TestProc;
- VAR
- aFoo: TFoo;
- aFoo2: TFoo2;
- BEGIN
- aFoo2 := TFoo2(aFoo);
- END;
-
- I find this to be a poor choice of example and have little sympathy with anyone
- who gets into trouble this way. (In fact, many optimizing compilers would
- issue an error or warning at this reference to an obviously random value.)
- More likely, a variable initialized to NIL would be cast and assigned to
- another; this should definitely be legal! To wit:
-
- PROCEDURE TestProc;
- VAR
- aFoo: TFoo;
- aFoo2: TFoo2;
- BEGIN
- aFoo := NIL;
- aFoo2 := TFoo2(aFoo);
- END;
-
- This should be legal, since NIL is a legal value for both classes. The same
- reasoning applies to member(NIL, someClass), although I maintain that this
- should be TRUE, not false, for all classes by the same reasoning: NIL is a
- legal value for every class!
-
- Third observation: the following example does have an unambiguous meaning and
- should be legal:
-
- TFoo(aFoo2) := aFoo;
-
- The left hand side of the assignment is fine as long as the cast is proper: it
- need only evaluate to the address of an object (in the loose sense of "object")
- of a compatible data type into which the value of the right hand side can be
- stuffed. Pascal is stickier on this point than C in that C allows any
- arbitrary expression on the lhs, but there is no need to carry this to the
- extreme of not allowing the above cast.
-
- There are other areas of OP which suffer from hokey semantics as well, but
- typecasting of object handles is one of the most visible and should be cleaned
- up as soon as possible.
-
- Jeff Alger
- Peat, Marwick, Main & Co.
-
-